<?php

namespace app\admin\controller;

use app\common\BaseHelper;

use think\Request;

/**
 * 小程序 通用设置
 */
class Wxcommon extends Base
{

    public function index() {
        $tenantId = session('TENANT_ID');
        $store = model('Store')->where(['tenantId'=>$tenantId])->order('createDate')->find();
        if(empty($store)) {
            return $this->error('请先添加门店','admin/store/index',2);
        }

        $setting = db('wxcommon')->where(['tenantId' => $tenantId])->find();
        if(!$setting){
            $setting = [];
        }

        $this->assign('setting', $setting);
        return $this->fetch();
    }

    public function save() {
        $request = Request::instance();
        if($this->request->isAjax()) {
            $model = db('wxcommon');
            $data = $request->param();
            $data['tenantId'] = session('TENANT_ID');

            if($data['id']){
                $data['createDate'] = time();
                $data['createOper'] = session('member')['id'];

                $result = $model->where(['id' => $data['id']])->update($data);
            }else{
                $data['id'] = BaseHelper::getUUID();
                $data['updateDate'] = time();
                $data['updateOper'] = session('member')['id'];

                $result = $model->insert($data);
            }

            if($result !== false){
                return json(['status' => 1, 'msg' => '保存成功！']);
            }else{
                return json(['status' => 0, 'msg' => '保存失败！']);
            }
        } else {
            return json(['status' => 0, 'msg' => '请求方式不正确！']);
        }
    }
}